package org.aurora.core.server.scheduler;

import org.aurora.common.util.NetworkUtil;

import java.util.Set;

public class SchedulerConfig {
    /**
     * 调度器与客户端交互的端口号
     */
    private volatile int schedulerPort = 8000;
    /**
     * 主调度器与其他调度器交互的端口号
     */
    private volatile int selfMainSchedulerPort = 9000;
    /**
     * 主调度器是否与客户端发起调度交互
     */
    private volatile boolean selfMainSchedulerEnabledWork = true;

    /**
     * 连接到主调度器超时时间
     */
    private volatile int connectMainSchedulerTimeout = 5000;

    /**
     * 业务配置 url
     */
    private volatile String bizConfigLoadUrl = "classpath:biz/application.properties";

    /**
     * 快速调度队列提前进入时间
     */
    private volatile long fastSchedulerQueueAdvanceTime = 10000L;

    private final SchedulerRuntimeConfig runtimeConfig = new SchedulerRuntimeConfig();

    public long getFastSchedulerQueueAdvanceTime() {
        return fastSchedulerQueueAdvanceTime;
    }

    public void setFastSchedulerQueueAdvanceTime(long fastSchedulerQueueAdvanceTime) {
        this.fastSchedulerQueueAdvanceTime = fastSchedulerQueueAdvanceTime;
    }

    public int getConnectMainSchedulerTimeout() {
        return connectMainSchedulerTimeout;
    }

    public void setConnectMainSchedulerTimeout(int connectMainSchedulerTimeout) {
        this.connectMainSchedulerTimeout = connectMainSchedulerTimeout;
    }

    public int getSchedulerPort() {
        return schedulerPort;
    }

    public void setSchedulerPort(int schedulerPort) {
        this.schedulerPort = schedulerPort;
    }

    public int getSelfMainSchedulerPort() {
        return selfMainSchedulerPort;
    }

    public void setSelfMainSchedulerPort(int selfMainSchedulerPort) {
        this.selfMainSchedulerPort = selfMainSchedulerPort;
    }

    public boolean isSelfMainSchedulerEnabledWork() {
        return selfMainSchedulerEnabledWork;
    }

    public void setSelfMainSchedulerEnabledWork(boolean selfMainSchedulerEnabledWork) {
        this.selfMainSchedulerEnabledWork = selfMainSchedulerEnabledWork;
    }

    public String getBizConfigLoadUrl() {
        return bizConfigLoadUrl;
    }

    public void setBizConfigLoadUrl(String bizConfigLoadUrl) {
        this.bizConfigLoadUrl = bizConfigLoadUrl;
    }

    /**
     * 运行时的配置
     */
    private class SchedulerRuntimeConfig {
        /**
         * 调度器 ip
         */
        private volatile String schedulerIp = NetworkUtil.getIpAddress();
        /**
         * 调度 hash 槽
         */
        private volatile Set<Integer> schedulerHashSlots;

        /**
         * 主调度器 ip
         */
        private volatile String mainSchedulerIp;

        /**
         * 主调度器端口号
         */
        private volatile int mainSchedulerPort;
    }

    public String getSchedulerIp() {
        return this.runtimeConfig.schedulerIp;
    }

    public Set<Integer> getSchedulerHashSlots() {
        return this.runtimeConfig.schedulerHashSlots;
    }

    public void setSchedulerHashSlots(Set<Integer> schedulerHashSlots) {
        this.runtimeConfig.schedulerHashSlots = schedulerHashSlots;
    }

    public String getMainSchedulerIp() {
        return this.runtimeConfig.mainSchedulerIp;
    }

    public void setMainSchedulerIp(String mainSchedulerIp) {
        this.runtimeConfig.mainSchedulerIp = mainSchedulerIp;
    }

    public int getMainSchedulerPort() {
        return this.runtimeConfig.mainSchedulerPort;
    }

    public void setMainSchedulerPort(int mainSchedulerPort) {
        this.runtimeConfig.mainSchedulerPort = mainSchedulerPort;
    }

}
